#include <iostream>
#include <unistd.h>
#include <stdint.h>
#include <sys/timerfd.h>

int main()
{
    //创建定时器
    int timefd = timerfd_create(CLOCK_MONOTONIC,0);

    //设置定时时间
    struct itimerspec itm;
    //设置初始超时时间
    itm.it_value.tv_sec = 3;
    itm.it_value.tv_nsec = 0;       //纳秒部分
    //设置周期超时时间
    itm.it_interval.tv_sec = 2;
    itm.it_interval.tv_nsec = 0;    //纳秒部分

    //启动定时器
    timerfd_settime(timefd,0,&itm,NULL);

    while(true)
    {
        uint64_t tmp;

        //读取8字节数据
        int ret = read(timefd,&tmp,sizeof(tmp));
        if(ret < 0)
        {
            return -1;
        }

        std::cout << "距离上次读取超时了" << tmp << "次" << std::endl;
        sleep(1);
    }
    return 0;
} 